package cn.ecnu.controller;


import cn.ecnu.constant.ResponseStatus;
import cn.ecnu.entity.Car;
import cn.ecnu.entity.Employee;
import cn.ecnu.service.CarService;
import cn.ecnu.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RequestMapping("/car")
@RestController
public class CarController {

    @Autowired
    private CarService carService;

    @GetMapping("/list")
    public R getCarList() {
        List<Car> result = carService.list();
        return R.ok().put("data", result);
    }

    @PostMapping("/insert")
    public R insertCar(@RequestBody Car car) {
        car.setId(ResponseStatus.uuidGenerator());
        boolean result = carService.save(car);
        if (result) return R.ok().put("data", car);
        return R.error(ResponseStatus.INTERNAL_ERROR, "服务器内部错误");
    }

    @PostMapping("/update")
    public R updateCar(@RequestBody Car car) {
        boolean result = carService.updateById(car);
        if (result) return R.ok().put("data", car);
        return R.error(ResponseStatus.INTERNAL_ERROR, "服务器内部错误");
    }

    @GetMapping("/{id}")
    public R getCarById(@PathVariable("id") String id) {
        Car car = carService.getById(id);
        return R.ok().put("data", car);
    }


    @GetMapping("/delete/{id}")
    public R deleteById(@PathVariable("id") String id) {
        boolean result = carService.removeById(id);
        if (result) return R.ok();
        return R.error(ResponseStatus.INTERNAL_ERROR, "服务器内部错误");
    }


    // 查询所有位置为location, 并且空闲的车辆
    @GetMapping("/location/{location}")
    public R getCarByLocation(@PathVariable String location) {
        List<Car> holders = carService.list(new QueryWrapper<Car>()
                .eq("location", location).eq("free", 1));
        System.out.println(holders);
        return R.ok().put("data", holders);
    }
}
